//
//  GuideView.m
//  IconMaster
//
//  Created by mac on 2017/8/2.
//  Copyright © 2017年 yangweiwei. All rights reserved.
//

#import "GuideView.h"
@interface GuideView()<UIScrollViewDelegate>{

    NSInteger totalImage;
}
 
@end
@implementation GuideView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //
        [self setUI];
    }
    return self;
}
- (void)setUI{

    UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight)];
    for (int i=0; i<4; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"GuidePage_%d",i+1]];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(KWidth * i, 0, KWidth, KHeight)];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        // 在最后一页创建按钮
        if (i == 3) {
            // 必须设置用户交互 否则按键无法操作
            imageView.userInteractionEnabled = YES;
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(KWidth / 3, KHeight * 7 / 8, KWidth / 3, KHeight / 16);
            // [button setTitle:@"点击进入" forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"立即体验-拷贝-3"] forState:UIControlStateNormal];
            // [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];/
            // button.layer.borderWidth = 2;
            //button.layer.cornerRadius = 5;
            //button.clipsToBounds = YES;
            //button.layer.borderColor = [UIColor whiteColor].CGColor;
            [button addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
            [imageView addSubview:button];
        }
        imageView.image = image;
        [myScrollView addSubview:imageView];
    }
    myScrollView.bounces = NO;
    myScrollView.pagingEnabled = YES;
    myScrollView.showsHorizontalScrollIndicator = NO;
    myScrollView.showsVerticalScrollIndicator = NO;
    myScrollView.contentSize = CGSizeMake(KWidth * 4, KHeight);
    myScrollView.delegate = self;
    [self addSubview:myScrollView];
    
   self.myPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0.0, KHeight - 50 , KWidth, 0.0)]; 
    self.myPageControl.numberOfPages = 4;
    self.myPageControl.currentPage =0;// 当前页数，第四页 
    [self addSubview:self.myPageControl]; 
    //实例化_timer;
//    totalImage
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(processTimer) userInfo:nil repeats:YES];

}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // 计算当前在第几页
     self.myPageControl.currentPage = (NSInteger)(scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width);
    if (self.myPageControl.currentPage == 4) {
        [self removeFromSuperview];
    }
}

// 点击按钮保存数据并切换根视图控制器
- (void) go:(UIButton *)sender{
    // 切换根视图控制器
//    TabBarViewController*log = [[TabBarViewController alloc] init];
//    [self.navigationController pushViewController:log animated:YES];
    [self removeFromSuperview];
    if ([self.delegate respondsToSelector:@selector(guideViewButton)]) {
        [self.delegate guideViewButton];
    }
    
}
- (void)processTimer{

    
}
 
@end
